define(['text!html/admin.html', 'css!styles/admin.css', 'js/navigationManager', 'js/serverWrapper', 'knockoutjs'], function (adminTemplate, _style, navigationManager, serverWrapper, ko) {
    return new function () {
        var presenter = this;

        var authenticationPresenterPromise = curl('js/authenticationPresenter');
        function airlineViewModel(airline) {
            var self = this;

            self.id = ko.observable(airline.airline_id);
            self.name = ko.observable(airline.name);
            self.isExpanded = ko.observable(false);
            self.onDelete = $.Callbacks();

            self.airlineClick = function () {
                self.isExpanded(!self.isExpanded());
            };

            self.deleteClick = function () {
                serverWrapper
                    .deleteAirline(self.id())
                    .success(function (result) {
                        if (!result || result.status == 0) return;
                        self.onDelete.fire(self);
                    });
            };
        };

        function adminViewModel() {
            var self = this;

            self.airlines = ko.observableArray();

            self.insertClick = function () {
                curl('js/airlinePresenter', function (airlinePresenter) {
                    airlinePresenter.insert();
                });
            };

            self.logoff = function () {
                authenticationPresenterPromise.then(function (authenticationPresenter) {
                    authenticationPresenter.logoff();
                });
            };

            self.removeAirline = function (airline) {
                self.airlines.remove(airline);
            };

            serverWrapper
                .getAllAirlines()
                .success(function (result) {
                    if (!result || result.length == 0) return;
                    result.forEach(function (airline) {
                        var airlineModel = new airlineViewModel(airline);
                        airlineModel.onDelete.add(self.removeAirline);
                        self.airlines.push(airlineModel);
                    });
                });
        };

        presenter.bind = function () {
            navigationManager.load('admin', adminTemplate, new adminViewModel());
        };
    };
});